﻿#ifndef NSA3000TWFRULE_H
#define NSA3000TWFRULE_H

#include <QtCore/qglobal.h>

#if defined(NSA3000TWFRULE_LIBRARY)
#  define NSA3000TWFRULESHARED_EXPORT Q_DECL_EXPORT
#else
#  define NSA3000TWFRULESHARED_EXPORT Q_DECL_IMPORT
#endif


#include "TABLEDEF.h"
#include "NSA3000TDB.h"
#include "DATATYPE.h"
#include "DATATYPE_S.h"
#include <QMap>
#include "seq_define.h"

#include <QtCore>//����qvector

#include <list>
#include <vector>
using namespace std;
#include <algorithm>
#include <functional>
#pragma pack(1)
typedef	struct _WF_RULE_
{
	char		RelativeDevByName[BYNAME_LEN];		        //��ص㸸�豸����
	quint8		RelativeType;								//��ص��ID
	int			RelativeIndex;								//��ص���
	quint8		RelativeState;								//ң������< > =
	float		RelativeValue;	                            //��ص�Ҫ��ֵ
	quint8      CloseType;                                  //��������������

	_WF_RULE_()
	{
		memset(RelativeDevByName,0,BYNAME_LEN);
		RelativeType=0;
		RelativeIndex=0;	
		RelativeState=0;	
		RelativeValue=0;	 
		CloseType=0;
	}

	~_WF_RULE_()
	{		
	}

} WF_RULE;

typedef	struct _WF_SINGLE_
{
	int         Sect;
	int         Step;
	WF_RULE     single_rule;
	bool        bOK;
	_WF_SINGLE_()
	{		
		Sect=0;
		Step=0;
		bOK=0;
	}
	~_WF_SINGLE_()
	{		
	}
}WF_SINGLE;

typedef	struct _WF_PRE_
{
	int         Dev_ID;                                 //�豸ID
	int			Index;								    //��ص���
	XUCHAR 		CurState;								//��ص�ֵ
}WF_PRE;

#pragma pack()
typedef QMap<qint64,WF_RULE>  WF_RULE_MAP;
typedef WF_RULE_MAP::Iterator ITR_MAP;

typedef QVector<WF_SINGLE>  VECTOR_SINGLE;//ȡ��ԭ��������
typedef VECTOR_SINGLE::iterator ITR_SINGLE;

typedef list<WF_PRE> LIST_PRE;//��Կ��ǰУ�������
typedef LIST_PRE::iterator ITR_PRE;

class IsInCheck
{
public:
	int CurID;
	int Index;
	IsInCheck(int ID,int Idx)
	{
		CurID=ID;
		Index=Idx;
	}
public:
	bool operator () (const WF_PRE &Para) 
	{
		return (Para.Dev_ID==CurID && Para.Index==Index);
	}
};

class NSA3000TWFRULESHARED_EXPORT NSA3000TWFRULE 
{
public:
    NSA3000TWFRULE();
	WF_RULE_MAP m_ruleLib;
	VECTOR_SINGLE  m_Vector_Single;
	LIST_PRE       m_List;
	QList<WF_RULE_SIMPLE *>  m_Rule[MAX_ZU_NUM];

public:
	bool   m_pSysLib;
	bool   m_bInited;

	bool    Init_wfrule_dll(NSA3000TDB* db);
	void    free_wfrule_dll();
	bool    WF_ASSERT(int Ticket_Type,int DevID,int index,XUCHAR  ForSta,XUCHAR  AftSta ,QString &hint,XUCHAR  &WFErrorType,bool IsBeforeDownCheck=FALSE,bool Is_zxwf_per_step=FALSE,bool bwf_commerror_forbid=FALSE);
	bool    ProcessWfRule(WF_RULE rule,QString &msg,bool IsBeforeDownCheck=FALSE,bool Is_zxwf_per_step=FALSE,bool bwf_commerror_forbid=FALSE);
	XUCHAR     GetYXRecordVal(int index,int DevID,QString  RelativeDevByName,bool Is_zxwf_per_step=FALSE);
	XUCHAR     GetYCRecordVal(int index,int DevID,QString  RelativeDevByName);
	QString GetHandSwitchOperString(int OperType);
	int     GetHandSwitchOperType(int relstate,int rulestate);

	//(�����Ʊĳһ�����)����Ӧ�����鲽����Ĺ���Ԫ���Ƿ�͸�Ԫ��(����Ʊ��ĳһ����)��ͬ
	//��Ʊʱ�ã��ڲ�����ProcessWfRule_YX_Relativeѭ��������鲽��int relDevID,int relDotNo�Ƚ�
	bool    WF_ASSERT_YX_Relative(int Ticket_Type,int DevID,int index,XUCHAR  ForSta,XUCHAR  AftSta ,int relDevID,int relDotNo);
	bool    ProcessWfRule_YX_Relative(WF_RULE rule,int relDevID,int relDotNo);

	//�������Ĵ�ȡ
	void	Add_Vector(int _Sect,int _Step,WF_RULE _Single,bool bOK);
	void	Clear_Vector();
	void	Vector_Test();
	void    GetSingleWFRuleInf(int _Ticket_Type,int _DevID,int _index,XUCHAR  _ForSta,XUCHAR  _AftSta,bool Is_zxwf_per_step=FALSE);

	int  Get_JXYXRecord_By_DevID(int Dev_ID,rYxDefine_Info &yx_record);

	//�м�̬�Ĵ洢����Կ��ǰУ�������
	void Add_List(int Ticket_Type,int DevID,int index,XUCHAR  ForSta,XUCHAR  AftSta);
	bool Find_List(int Ticket_Type,int DevID,int index,XUCHAR  & Value);
	void Clear_List();

	

	void RuleStructToLineList(rWf_Rule_Info *m_pAllRule,int m_Rule_Count,QStringList &sTextRuleList);
	void RuleTextToChinese(QStringList sTextRule,QStringList &sChineseRuleList);

	QString ExtractIndexName(QString Record,int state);
	int  GetRuleTypeOnOrOff(QString srule);

	bool DeCodeRulePerLine(QString List);
	void ClearRuleList();

	QStringList checkFormatperline(QString stemp,int &ErrorCount);
	void   CompiletextList(QStringList sTextRuleList,QStringList &sRetErrorList,int &RetErrorCount,int &RetWarnCount);
	QStringList checkIndexName(QString stemp,QString sSaveLine,WF_PARAM_YX_INDEX* pIndex,int nCount, int* Exist ,QString &sMainIndexName,int &ErrorCount);
	bool CheckRulePerLine(QString List,QString sCurALlList,QStringList &sSignError);
	void InsertTextRuleToDatabase(  bool bsingle,QStringList sTextRuleList);

	bool TextRuleListFileInsertDatabase(QString sFileName,QStringList &sRetRuleList,QStringList &sRetErrorList,int &RetErrorCount,int &RetWarnCount);
	void DatabaseOuttoTextRuleListFile(QString sFileName);

private:
	qint64 Encode(int rtu,int dot,int F_op,int A_op,int sect,int step);
};


#endif // NSA3000TWFRULE_H
